package org.OpenGeoPortal.LayerData;

import java.io.File;
import java.io.IOException;

/**
 * ingest a set of files into Solr
 * @author smcdon08
 *
 */
public class SolrIngestor 
{
	public static void main(String[] args)
	{
		int errorCount = 0;
		//String solrDirectoryName = "/Users/smcdon08/tmp/OpenGeoPortal/HarvardSolrData/";
		String solrDirectoryName = "/Users/smcdon08/tmp/OpenGeoPortal/TuftsSolrData/";
		//String solrDirectoryName = "/usr/local/tomcat/current/sites/solr/ingest/harvard/";
		//String hostname = "localhost";
		String hostname = "geoportal-dev.atech.tufts.edu";
		String ingestCommand = "curl http://" + hostname + "/solr/update -F stream.file=";
		
		/*
		if (args.length > 0)
			hostname = args[0];
		if (args.length > 1)
			solrDirectoryName = args[1];
*/
		File solrDirectory = new File(solrDirectoryName);
		File[] solrFiles = solrDirectory.listFiles();
		for (int i = 0  ; i < solrFiles.length ; i++)
		{
				File currentFile = solrFiles[i];
				if (currentFile.getName().endsWith(".xml"))
				{
					try
					{
						System.out.println("ingesting " + currentFile);
						Runtime runtime = Runtime.getRuntime();
						Process process = runtime.exec(ingestCommand + currentFile.toString());
						int returnValue = process.waitFor();
						if (returnValue != 0)
							System.out.println("  unexected return value " + returnValue + " for " + currentFile);
							System.out.println("  exec = " + ingestCommand + currentFile.toString());
						System.gc();
					}
					catch (IOException e) 
					{
						System.out.println("IO error handling file " + currentFile);
						e.printStackTrace();
						errorCount++;
						
					} 
					catch (InterruptedException e) 
					{
						System.out.println("Interrupted error handling file " + currentFile);
						e.printStackTrace();
						errorCount++;
					}
					catch (Exception e)
					{
						System.out.println("other exception:");
						e.printStackTrace();
						errorCount++;
					}	
				}

		}
		System.out.println("the end of processing, errorCount = " + errorCount);
	}

}
